package com.liushuai.eas.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liushuai.eas.common.Result;
import com.liushuai.eas.pojo.Notify;
import com.liushuai.eas.service.NotifyImp;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/notify")
public class NotifyController {

    @Resource
    private NotifyImp notifyImp;

    @PostMapping
    public Result<Notify> add(@RequestBody Notify notify){
        if (notifyImp.insert(notify)!=1)
            return Result.fail();
        return Result.success(notify);
    }

    @PutMapping
    public Result<Notify> update(@RequestBody Notify notify){
        if (notifyImp.updateById(notify)!=1)
            return Result.fail(notify);
        return Result.success(notify);
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        Page<Notify> notifyPage = notifyImp.selectPage(new Page<Notify>(pageNum, pageSize), Wrappers.<Notify>lambdaQuery().like(Notify::getTitle, search));
        return Result.success("数据加载成功",notifyPage);
    }

    @DeleteMapping
    public Result<Notify> delete(@RequestBody Notify notify){
        if (notifyImp.deleteById(notify)!=1)
            return Result.fail(notify);
        return Result.success(notify);
    }

    @GetMapping(value = "/queryShowNotifyList")
    public Result<List<Notify>> queryShowNotifyList(){
        List<Notify> notifies = notifyImp.queryShowNotifyList();

        return Result.success(notifies);
    }


}
